package com.cda.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: PdfExport
 * @Auther: zhangyingqi
 * @Date: 2018/10/8 20:21
 * @Description:
 */
public class PdfExport extends PdfPageEventHelper{
    protected static final Logger logger = LoggerFactory.getLogger(PdfExport.class);

    public PdfTemplate tpl;
    public BaseFont bf;

    public static void addPdfPage(String filename, String outname, int startPage, int pageNumber){
        logger.info("PDF起始页码："+startPage);
        logger.info("PDF+材料总页码："+pageNumber);
        int realnum = 0;
        try{
            //读取文件
            PdfReader pdfReader = new PdfReader(filename);
            //生成文件
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(outname));
            for(int i=1; i<= pdfReader.getNumberOfPages(); i++)
            {
                PdfContentByte content = pdfStamper.getOverContent(i);
                //if(i == pdfReader.getNumberOfPages()){
                //添加文字
                BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                content.beginText();
                content.setFontAndSize(font, 20);
                content.setTextMatrix(200, 200);
                realnum = i+startPage;
                logger.info("第"+ realnum +"页/共"+ pageNumber +"页");
                content.showTextAligned(Element.ALIGN_LEFT,"第" + realnum + "页/共" + pageNumber + "页",pdfReader.getPageSize(i).getWidth()/2-10,20,0);//左边距、下边距
                content.endText();
                //}
            }
            pdfStamper.close();
            pdfReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public void onOpenDocument(PdfWriter writer, Document document) {
        try {
            tpl = writer.getDirectContent().createTemplate(100, 100);
            bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }

    public void onEndPage(PdfWriter writer, Document document) {
        // 在每页结束的时候把“第x页”信息写道模版指定位置
        PdfContentByte cb = writer.getDirectContent();
        cb.saveState();
        String text = "第" + writer.getPageNumber() + "页,共";
        cb.beginText();
        cb.setFontAndSize(bf, 8);
        cb.setTextMatrix(460, 786);// 定位“第x页,共” 在具体的页面调试时候需要更改这xy的坐标
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, 492, 786);// 定位“y页” 在具体的页面调试时候需要更改这xy的坐标

        cb.saveState();
        cb.stroke();
        cb.restoreState();
        cb.closePath();// sanityCheck();
    }

    public void onCloseDocument(PdfWriter writer, Document document) {
        // 关闭document的时候获取总页数，并把总页数按模版写道之前预留的位置
        tpl.beginText();
        tpl.setFontAndSize(bf, 8);
        tpl.showText(Integer.toString(writer.getPageNumber() - 1) + "页");
        tpl.endText();
        tpl.closePath();// sanityCheck();
    }

}
